//
//  ProgressLabyer.m
//  IOSUI思路
//
//  Created by 崔博 on 2018/9/26.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "ProgressLabyer.h"

@implementation ProgressLabyer

- (instancetype)initWithLayer:(id)layer
{
    if (self == [super initWithLayer:layer]) {
        if ([layer isKindOfClass:[ProgressLabyer class]]) {
            ProgressLabyer * otherLyer = layer;
            self.proress = otherLyer.proress;
            self.startAngle = otherLyer.startAngle;
            self.tinColor = otherLyer.tinColor;
            self.trackColor = otherLyer.trackColor;
        }
    }
    return self;
}
+ (BOOL)needsDisplayForKey:(NSString *)key
{
    if ([key isEqualToString:@"progress"]) {
        return YES;
    }else
    {
        return [super needsDisplayForKey:key];
    }
}
- (void)drawInContext:(CGContextRef)ctx
{
    CGFloat radius = MIN(self.bounds.size.width, self.bounds.size.height) / 2.0;
    CGPoint center = {self.bounds.size.width / 2,self.bounds.size.height / 2};
    CGRect cirleRect = {center.x - radius, center.y - radius, radius * 2.0, radius * 2.0};
    // 绘制背景
    CGContextAddEllipseInRect(ctx, cirleRect);
    CGContextSetFillColorWithColor(ctx, self.trackColor.CGColor);
    CGContextFillPath(ctx);
    
    // 绘制弧线
    CGContextAddArc(ctx, center.x, center.y, radius, self.startAngle, self.startAngle + self.proress * 2.0 * M_PI, 0);
    CGContextAddLineToPoint(ctx, center.x, center.y);
    CGContextClosePath(ctx);
    CGContextSetFillColorWithColor(ctx, self.tinColor.CGColor);
    CGContextFillPath(ctx);
    
}
@end
